package org.xg.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName OutServlet
 * @date 2021/9/24 9:18
 **/
@WebServlet("/outServlet")
public class OutServlet extends HttpServlet {
    private static final long serialVersionUID=1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        run1(resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    /**
     * 使用字节流向页面输出
     * @param response
     * @throws IOException
     */
    public void run1(HttpServletResponse response) throws IOException {
        response.setHeader("Content-TYpe","text/html;charset=UTF-8");
        OutputStream outputStream = response.getOutputStream();
        byte []bytes = "编程帮 www.biancheng.net".getBytes("UTF-8");
        outputStream.write(bytes);
    }

    /**
     * 使用字符流向页面输出
     * @param response
     * @throws IOException
     */
    public void run2(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("编程帮 www.biancheng.net");
    }
}
